<?php

namespace App\Http\Controllers\Api;

use App\Models\RobotOrder;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Symfony\Component\HttpFoundation\Response;

class PFPayController extends Controller
{
    private $PAYID = 'be598cc7-c22b-42f7-a465-60ced3369a7a'; //商户appid
    private $PAYKEY = 'ga2ig2ng9lbbm1f1s7j0fhju4d144507'; //商户秘钥
    private $PAYURL = 'http://business.neubupay.com/api/api/pay'; //请求地址
    private $UID = '20656'; //请求地址

    //跳转支付页面
    public function pay(Request $request)
    {
        $order = RobotOrder::where('order_no', $request->order_no)->first();
        if (!isset($order)) {
            return error('订单号错误');
        }

        // $payOrder = PayOrder::where('order_id', $request->get('order_id'))->first();
        $data = [
            'appid' => $this->PAYID,
            'order_no' => $request->order_no,
            'notify_url' => config('pfpay.notify_url'),
            'return_url' => config('pfpay.return_url'),
            'remark' => '购买机器',
            'amount' => number_format($order->price, 2, '.', ''),
            'uid' => $this->UID, //如实填写,否则会造成无法支付
            // 'type' => $request->type
        ];
        $sign = $this->sign($data, $this->PAYKEY);
        $data['sign'] = $sign;

        $sHtml = "<form id='alipaysubmit' name='alipaysubmit' action='" . $this->PAYURL . "' method='post'>";
        foreach ($data as $key => $val) {
            $sHtml .= "<input type='hidden' name='" . $key . "' value='" . $val . "'/>";
        }
        $sHtml = $sHtml . "<input type='submit' value='loading...'></form>";
        $sHtml = $sHtml . "<script>document.forms['alipaysubmit'].submit();</script>";
        return Response::create($sHtml);
    }

    //异步通知
    public function notify(Request $request)
    {
        \Log::info($request);
        \Log::info('支付回调');
        return;
        $res = PayType::query()->where('payFile', 'new_ali_pay')->first();
        if ($res->status == 0) {
            return false;
        }

        $data['appid'] = $request->post("appid");
        $data['order_no'] = $request->post("order_no");
        $data['order_id'] = $request->post("order_id");
        $data['amount'] = $request->post("amount");
        $data['time'] = $request->post("time");

        $sign = $this->sign($data, $this->PAYKEY);

        if ($this->PAYID == $data['appid'] && $sign == $request->post("sign")) {

            recharge($request->post('order_no'), $request->post('amount'), $request->post('order_no'));
        }
        echo 'success';
    }


    function sign($data, $key)
    {
        $data = $this->argSort($data);
        $data = $this->createLinkstring($data);
        $sign = strtoupper(md5($data . '&key=' . $key));
        return $sign;
    }

    function argSort($para)
    {

        ksort($para);
        reset($para);
        return $para;
    }

    function createLinkstring($para)
    {
        $arg = "";

        foreach ($para as $key => $val) {
            if (!is_null($val)) {
                $arg .= $key . "=" . $val . "&";
            }
        }

        $arg = substr($arg, 0, strlen($arg) - 1);

        if (get_magic_quotes_gpc()) {
            $arg = stripslashes($arg);
        }

        return $arg;
    }
}
